package com.eazy.lksy.common;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.eazy.lksy.utils.ActionRequest;

/**
 * controller 基础操作类
 * 
 * @author jzx
 * @date 2016.3.19
 */
public abstract class BaseController {

	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected HttpSession session;

	@ModelAttribute
	private void init(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession();
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public HttpSession getSession() {
		return session;
	}
	
	public String getPara(String key) {
		return ActionRequest.getPara(key, request);
	}

	public String getUTF8Para(String key) {
		return ActionRequest.getUTF8Para(key, request);
	}

	public Map<String, String> getRequest() {
		return ActionRequest.actionRequest(request);
	}

	public String[] getParaValues(String key) {
		return ActionRequest.getParaValues(key, request);
	}

	public void setAttr(String key, Object value) {
		ActionRequest.setAttr(request, key, value);
	}

	public void setAttrs(Map<String, Object> map) {
		ActionRequest.setAttrs(request, map);
	}

	public Object getAttr(String key) {
		return ActionRequest.getAttr(request, key);
	}

	public void renderJson(String data) {
		ActionRequest.renderJson(data, response);
	}

	public void renderWord(String fileName, String columnName, List<String[]> list1) {
		ActionRequest.renderWord(fileName, columnName, list1, response);
	}

	public void renderExcel(String fileName, String columnName, List<String[]> list1) {
		ActionRequest.renderExcel(fileName, columnName, list1, response);
	}

	public void renderPdf(String fileName, String columnName, List<String[]> list1) {
		ActionRequest.renderPdf(fileName, columnName, list1, response);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				// setValue(text == null ? null :
				// StringEscapeUtils.escapeHtml(text.trim()));
				setValue(text == null ? null : text.trim());
			}

			@Override
			public String getAsText() {
				Object value = getValue();
				return value != null ? value.toString() : "";
			}
		});
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				try {
					setValue(DateUtils.parseDate(text));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@ExceptionHandler(Exception.class)
	public String handleException(Exception ex, HttpServletRequest request) {
		ex.printStackTrace();
		request.setAttribute("exMsg", ex.getMessage());
		return "errors/exception";
	}
}
